﻿@using admin.Model
@{
    ViewData["Title"] = "AuthorEdit";
    Layout = "~/Views/Shared/_SmallWinLayout.cshtml";
    var author = Model as Author;
}

<div class="page-container">
    <div class="form form-horizontal">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>作者ID：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@author.Id" name="authorid" readonly="readonly">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>作者名：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@author.Name" name="authorname">
                <span class="my_error" style="display:none">*名字不能为空</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否签订合同：</label>
            <div class="formControls col-xs-8 col-sm-9">
                @if (author.IsContact.Equals("1"))
                {
                    <input type="text" class="input-text auth" value="已签" name="contact" maxlength="2">
                }
                else
                {
                    <input type="text" class="input-text auth" value="未签" name="contact" maxlength="2">
                }
                <span class="my_error" style="display:none">*请填写"已签"或"未签"</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">联系地址：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text auth" value="@author.Address" name="address">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" onclick="authorinfo('/Home/AuthorSaveInfo')" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                <button onClick="layer_close();" class="btn btn-primary radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //提交作者信息
    function authorinfo(url) {
        var infoList = new Array();
        $('input[name = authorid]').blur(function () {
            infoList.push($(this).val());
        });
        $('input[name = authorname]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '作者名称不能为空');
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
            else {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = contact]').blur(function () {
            val = this.value;
            if (val.match(/^(已签|未签)$/i)) {
                infoList.push($(this).val());
                $(this).next().hide();
                $(this).data({ 's': '1' });
            }
            else {
                $(this).next().show();
                $(this).data({ 's': '0' });
            }
        });
        $('input[name = address]').blur(function () {
            infoList.push($(this).val());
        });
        layer.confirm('确认保存？', function (index) {
            infoList.length = 0;
            $('.auth').blur();
            tot = 0;
            $('.auth').each(function () {
                if ($(this).data('s') == 1) {
                    tot += 1;
                }
            });
            if (tot != 2) {
                //alert(tot);
                layer.msg('信息填写存在错误，请重新编辑', { icon: 5, time: 2000 });
            }
            else if (tot == 2) {
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: { "infoitem": infoList.toString() },
                    success: function (response) {
                        if (response == "ok") {
                            layer.msg('信息已保存', { icon: 1, time: 3000 });
                            self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                                window.parent.location.reload();
                            }, 2000); //这里2000代表两秒
                        }
                        else if (response == "nochange") {
                            layer.msg('信息没有改变哦', { icon: 1, time: 3000 });
                            self.setInterval(function () {
                                //window.parent.location.reload();
                                layer_close();
                            }, 2000);
                        }
                        else if (response == "false") {
                            layer.msg('信息保存失败！请稍后再试', { icon: 5, time: 3000 });
                            infoList.length = 0;
                        }
                    },
                    error: function (response) {
                        layer.msg('信息保存失败！请重新编辑', { icon: 5, time: 3000 });
                        infoList.length = 0;
                    }
                });
            }

        });
    }
</script>